Skip to content

Escape keyword-named identifiers in the Python generator [patch] - #347

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/106-python-keyword-identifiers
Oct 30, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/106-python-keyword-identifiers

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Part of #106. This is the Python step of the split in the issue. Rust and C++ landed in #330. Go, JavaScript, C# and C are still open, so this PR deliberately doesn't close the issue.

What was wrong

PythonGenerator wrote every declared name verbatim. A name that is a Python hard keyword made the module fail with SyntaxError on import. An enumeration member called None, which is an ordinary C# name, was enough to trigger it, as were fields, parameters and locals named from, lambda, in, is, pass and so on.

Change

  • PythonGenerator now overrides ReservedWords with the hard keywords from keyword.kwlist. The soft keywords (match, case, type, _) are legal names, so they are left alone.
  • It uses the base EscapeIdentifier, which appends an underscore. That is a rename, and it is also PEP 8's spelling for this case.
  • IsReceiverWord keeps True, False and None as written when a reference means the literal.
  • Every declaration site now goes through EscapeIdentifier:
    • property names and static accessor names
    • aliases, enumerations and their members
    • fields, plus self.x initialisers and member initialisers
    • functions, methods and classes
    • parameters, locals, global lists and keyword arguments
  • References already go through EscapeReference, so declarations and uses stay in step.

Tests

ReservedWordIdentifierTests has two new tests:

  • Python_KeywordNamesRunAndResolve generates a class, an enum and a function whose names are Python keywords. It runs the module under python3 with a driver that reads each one back.
  • Python_ReferenceKeepsTheLiteralWordsAndEscapesTheMember checks that self.from becomes self.from_ while a bare None stays None.

Both tests fail with the generator change reverted (SyntaxError: invalid syntax) and pass with it. The full suite passes: 1545 tests, 0 failed.


Generated by Claude Code

A field, property, parameter, local, function, class, enumeration member
or alias named after a Python keyword came out verbatim, so the module
failed with SyntaxError on import. An enumeration member called None, an
ordinary C# name, was enough. Python now appends an underscore, the PEP 8
spelling for this, at each declaration, and references go through the
same hook so they agree. The literal words True, False and None are left
as written when a reference means them. Soft keywords (match, case, type)
are legal names and are not escaped.

Part of #106.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW6Nv9nDLvHQUanGyzZ
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants